package net.icapsid.counter.client;

import net.icapsid.counter.client.util.math.FloatMatrix;
import net.icapsid.counter.client.util.math.MatrixUtil;

import com.googlecode.gwtgl.binding.WebGLRenderingContext;
import com.googlecode.gwtgl.binding.WebGLUniformLocation;

public class DisplayMatrix {

	private int previousNumber = 0;
	private int currentNumber = 0; 

	private float translation[] = new float[3];
	
	private FlightControl flightControl = null;

	public DisplayMatrix(FlightControl fc){
		flightControl = fc;
	}
	
	public int getCurrentNumber() {
		return currentNumber;
	}

	public void setCurrentNumber(int num) {
		previousNumber = currentNumber;
		currentNumber = num;
	}
	
	public void setTranslation(float x, float y, float z){
		translation[0] = x;
		translation[1] = y;
		translation[2] = z;
		
	}

	void draw(WebGLRenderingContext glContext, int points, WebGLUniformLocation projectionMatrixUniform, FloatMatrix perspectiveMatrix, int index){
		
		FloatMatrix translationMatrix;
		FloatMatrix resultingMatrix;
		
		float separation = 2.0f;

		// Iterate over the rows
		for(int i = 0; i < 4; ++i){
			
			for(int j = 0; j < 7; ++j){
				if(config[currentNumber][j][i] == 0){

					// Previous run had something here but now we have nothing here
					if(config[previousNumber][j][i] == 1){
						flightControl.launchFrom(i * separation + translation[0], 
                                				-j * separation + translation[1],
                                				translation[2], index);
					}
					continue;
				}
				
				translationMatrix = MatrixUtil.createTranslationMatrix(i * separation + translation[0], 
						                                              -j * separation + translation[1],
						                                              translation[2]);
				
				resultingMatrix = perspectiveMatrix.multiply(translationMatrix);//.multiply(rotationMatrix);

				glContext.uniformMatrix4fv(projectionMatrixUniform, false, resultingMatrix.getColumnWiseFlatData());
				glContext.drawArrays(WebGLRenderingContext.TRIANGLES, 0, points);
			}
		}
	}


	private static final short[][][] config = new short[][][]{
											     // 0
												 {{1, 1, 1, 1},
												  {1, 0, 0, 1},
												  {1, 0, 0, 1},
												  {1, 0, 0, 1},
												  {1, 0, 0, 1},
												  {1, 0, 0, 1},
												  {1, 1, 1, 1}},
										          // 1
												  {{0, 0, 0, 1}, 
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1}},
												   // 2
												  {{1, 1, 1, 1}, 
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {1, 1, 1, 1},
												   {1, 0, 0, 0},
												   {1, 0, 0, 0},
												   {1, 1, 1, 1}},
												   // 3
												  {{1, 1, 1, 1}, 
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {1, 1, 1, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {1, 1, 1, 1}},
												   // 4
												  {{1, 0, 0, 1}, 
												   {1, 0, 0, 1},
												   {1, 0, 0, 1},
												   {1, 1, 1, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1}},
												  // 5	
												  {{1, 1, 1, 1}, 
												   {1, 0, 0, 0},
												   {1, 0, 0, 0},
												   {1, 1, 1, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {1, 1, 1, 1}},
											      // 6
												  {{1, 1, 1, 1}, 
												   {1, 0, 0, 0},
												   {1, 0, 0, 0},
												   {1, 1, 1, 1},
												   {1, 0, 0, 1},
												   {1, 0, 0, 1},
												   {1, 1, 1, 1}},
												   // 7
												  {{1, 1, 1, 1}, 
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1}},
												   // 8
												  {{1, 1, 1, 1}, 
												   {1, 0, 0, 1},
												   {1, 0, 0, 1},
												   {1, 1, 1, 1},
												   {1, 0, 0, 1},
												   {1, 0, 0, 1},
												   {1, 1, 1, 1}},
												   // 9
           										  {{1, 1, 1, 1}, 
												   {1, 0, 0, 1},
												   {1, 0, 0, 1},
												   {1, 1, 1, 1},
												   {0, 0, 0, 1},
												   {0, 0, 0, 1},
												   {1, 1, 1, 1}}
	};
	
}
